const Koa = require("koa");
const app = new Koa();
const koaBody = require("koa-body");

const routes = require("./routes");

app.use(require("koa-static")(__dirname + "/static"));
app.use(koaBody());

app.on("error", err => {
  console.log("error begin=========================");
  console.log(err);
  console.log("error end===========================");
});

// logger
app.use(async (ctx, next) => {
  const start = new Date();
  await next();
  const ms = new Date() - start;
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});

app.use(routes.routes(), routes.allowedMethods());
app.use(async (ctx, next) => {
  if (ctx.pass_result) {
    ctx.body = ctx.pass_result;
  }
});

app.listen(3000);
